package com.hung.trade.pay.mq.processer;

import com.alibaba.fastjson.JSON;
import com.hung.trade.common.protocol.OrderCancelMessage;
import com.hung.trade.common.rocketmq.IMessageProcesser;
import com.hung.trade.dao.mapper.TradePayMapper;
import com.hung.trade.pay.service.IPayService;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author hungki
 * @createTime 2018-10-09 下午 2:18
 */
public class PayOrderCancelProcesser implements IMessageProcesser {
    private static final Logger logger = LoggerFactory.getLogger(PayOrderCancelProcesser.class);
    @Autowired
    private TradePayMapper tradePayMapper;
    @Autowired
    private IPayService iPayService;
    private String groupName="pay_order_topic_cancel_group";

    @Override
    public boolean heandlerMessage(MessageExt messageExt) {
        try {
            String body =new String(messageExt.getBody(),"UTF-8");
            String topic = messageExt.getTopic();
            String msgId = messageExt.getMsgId();
            String keys = messageExt.getKeys();
            String tags = messageExt.getTags();
            logger.info(String.format("recive message [ body = %s ] ",body));

            OrderCancelMessage orderCancelMessage = JSON.parseObject(body,OrderCancelMessage.class);

            if(messageExt.getReconsumeTimes()>=3){
                logger.info("订单取消，重试三次任然失败，记录到数据库，消息标记为消费成功");
                return true;
            }

            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
